package com.fwzc.giuseppe.fragment;

import java.util.ArrayList;
import java.util.List;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.adapter.UniversalAdapter;
import com.fwzc.giuseppe.entity.Order;
import com.fwzc.giuseppe.util.ViewHolder;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class WaitPaymentFragment extends BasicFragment implements OnRefreshListener{
	private ListView myListView;
	private UniversalAdapter<Order> adapter;
	private List<Order> datas = new ArrayList<Order>();
	private ImageView style;
	private TextView name, code, price;
	private Button number, payment;
	private int order_id;
	private SwipeRefreshLayout refreshLayout;
	@SuppressLint("HandlerLeak") 
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == 1){
				refreshLayout.setRefreshing(false);
			}
		}
		
	};
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.wait_payment_fragment, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initView();
	}

	private void initView() {
		myListView = (ListView) getActivity().findViewById(
				R.id.wait_payment_list);
		refreshLayout = (SwipeRefreshLayout)getActivity().findViewById(R.id.refreshLayout_wait_payment);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark,android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);
		for (int i = 0; i < 6; i++) {
			Order order = new Order();
			order.order_id = i;
			order.clothes_name = "这是乔治白最漂亮的衣服哦，赶紧买";
			order.number = "X" + i;
			order.price = "￥ 80" + i;
			order.order_code = "订单号:41545431212121";
			datas.add(order);
		}
		adapter = new UniversalAdapter<Order>(getActivity(), datas,
				R.layout.payment_list_item) {
			@Override
			public void convert(ViewHolder viewHolder, Order order) {
				order_id = order.order_id;
				style = (ImageView) viewHolder.getView(R.id.clothes_image);
				name = (TextView) viewHolder.getView(R.id.name);
				code = (TextView) viewHolder.getView(R.id.order_code);
				price = (TextView) viewHolder.getView(R.id.price);
				number = (Button) viewHolder.getView(R.id.number);
				payment = (Button) viewHolder.getView(R.id.payment);
				name.setText(order.clothes_name);
				number.setText(order.number);
				price.setText(order.price);
				code.setText(order.order_code);
				initClick(order_id);
			}
		};
		myListView.setAdapter(adapter);
	}

	private void initClick(final int order_id) {
		payment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popNews(order_id + "付款");
			}
		});
	}

	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		handler.sendEmptyMessage(1);
	}
}
